import smtplib
from libs.Objects import Objects
import zmail


class EmailSender(object):

    def not_trading_date(self, date):
        print('not trading date')
        msg = {
            'subject': '%s 不是交易日' % (date),
            'content_text': '%s 不是交易日，开心玩耍吧！' % (date)
        }
        self.send(msg)

    def trading_data(self, date, tradingFile):
        if Objects.isEmpty(tradingFile):
            raise Exception('tradingFile is empty')
        print('trading date %s' % date)
        msg = {
            'subject': '截至 %s 交易数据' % (date),
            'attachments': [tradingFile]
        }
        self.send(msg)

    def trading_date(self, date, resultFile):
        if Objects.isEmpty(resultFile):
            raise Exception('resultFile is empty')
        print('trading date %s' % date)
        msg = {
            'subject': '%s 市场情绪表' % (date),
            'content_text': '%s 市场情绪表' % (date),
            'attachments': [resultFile]
        }
        self.send(msg)

    def send(self, msg):
        # 邮件内容

        # 发件人信息
        sender = ('290547770@qq.com', 'qmtkldfyactobibf')  # 注意：这里使用的是密码，如果使用QQ邮箱等需要开启SMTP服务并获取授权码
        # sender = ('jyworker@126.com', 'ULxYbcLQu5JLJNDf') 126只能授权半年

        # 收件人信息
        recipients = ['jyworker@126.com']  # 可以是单个收件人或收件人列表

        # 发送邮件
        server = zmail.server(*sender)
        if server.smtp_able():
            print('SMTP服务可用')
        try:
            server.send_mail(recipients, msg)
        except smtplib.SMTPResponseException as e:
            # ignore
            pass

        print('send successfully!')


if __name__ == '__main__':
    EmailSender().not_trading_date('2015-10-10')
